﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.Transaction
{
	public partial class NeedChargedForBuyForm : BaseForm
	{
		private List<PurchaseBill> purchaseBillList;

		public NeedChargedForBuyForm()
		{
			InitializeComponent();

			ucBuyBill.InitializeColumnHeader(typeof(Models.PurchaseBill));

			ucBuyBill.dgvData.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dgvData_DataBindingComplete);
			ucBuyBill.dgvData.CellFormatting += new DataGridViewCellFormattingEventHandler(dgvData_CellFormatting);

		}

		void dgvData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			string str = ucBuyBill.dgvData.Columns[e.ColumnIndex].Name;
			switch (str)
			{

				case "GoodID":
					{
						e.FormattingApplied = true;

						e.Value = purchaseBillList[e.RowIndex].Good.Code; break;
					}
				case "StaffID":
					{
						e.FormattingApplied = true;

						e.Value = purchaseBillList[e.RowIndex].Staff.Name; break;
					}
				case "CarTypeID":
					{
						e.FormattingApplied = true;

						e.Value = purchaseBillList[e.RowIndex].CarType.Name; break;
					}
			}
		}

		void dgvData_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
		{
			try
			{
				decimal total = 0;
				foreach (DataGridViewRow item in ucBuyBill.dgvData.Rows)
				{
					total += Convert.ToDecimal(item.Cells["TotalPrice"].Value);
				}

				lbBuyTotal.Text = total.ToString("f1") + "元";
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		
		private void btnClose_Click(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}

		private void NeedChargedForBuyForm_Load(object sender, EventArgs e)
		{
			try
			{
				DataBindForBuyBill();
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}

		}
		private void DataBindForBuyBill()
		{
			try
			{
				CarDBContext db = new CarDBContext();
				purchaseBillList = db.PurchaseBill.Include("Good").Include("CarType").Include("Staff").Where(p => p.IsSettle == "否").OrderBy(p => p.ID).ToList();
				ucBuyBill.DataSource = purchaseBillList;
			}
			catch (Exception ex)
			{

				throw ex;
			}
		}

		private void btnSettleSales_Click(object sender, EventArgs e)
		{
			try
			{
				if (ucBuyBill.dgvData.SelectedRows.Count > 0)
				{
					if (MessageBox.Show(this, "确定付款吗？", "确认", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
					{
						CarDBContext db = new CarDBContext();
						int id = Convert.ToInt32(ucBuyBill.dgvData.SelectedRows[0].Cells["ID"].Value);
						db.PurchaseBill.First(p => p.ID == id).IsSettle = "是";
						db.SaveChanges();

						DataBindForBuyBill();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}


		private void btnSettleAll_Click(object sender, EventArgs e)
		{
			try
			{
				if (ucBuyBill.dgvData.Rows.Count > 0)
				{
					if (MessageBox.Show(this, "确定全部付款吗？", "确认", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
					{
						CarDBContext db = new CarDBContext();
						foreach (DataGridViewRow item in ucBuyBill.dgvData.Rows)
						{
							int id = Convert.ToInt32(item.Cells["ID"].Value);
							db.PurchaseBill.First(p => p.ID == id).IsSettle = "是";
						}					
						
						db.SaveChanges();

						DataBindForBuyBill();
					}
				}
			}
			catch (Exception ex)
			{
				this.ShowInfo(ex.Message);
			}
		}
	}
}
